﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SalienceEngineDemo.Dialogs
{
    public partial class ExplainConcepts : Form
    {
        // Raw data from engine
        private string m_ConceptMatches;

        // This is the default minimum concept score
        private float m_fMinConceptScore = 0.45f;

        // List object for concept labels
        private List<string> m_ListConceptLabels = new List<string>();
        private int nListIndex = 0;

        public ExplainConcepts()
        {
            InitializeComponent();
        }

        public ExplainConcepts(string conceptMatchString)
        {
            m_ConceptMatches = conceptMatchString;
            InitializeComponent();
        }

        private void ExplainConcepts_Load(object sender, EventArgs e)
        {
            // Load the concept labels into sorted list
            foreach (string aLine in m_ConceptMatches.Split('\n'))
            {
                if (!aLine.StartsWith("\t") && aLine.Trim().Length>0)
                    m_ListConceptLabels.Add(aLine);
            }
            if (m_ListConceptLabels.Count > 0)
            {
                lblConceptLabel.Text = m_ListConceptLabels[nListIndex];
                CheckForMinScore(m_ListConceptLabels[nListIndex]);
                PopulateTermGrid(m_ListConceptLabels[nListIndex]);
                btnPrev.Enabled = false;
                btnNext.Enabled = true;
            }
        }

        private void btnPrev_Click(object sender, EventArgs e)
        {
            nListIndex--;
            if (nListIndex >= 0)
                lblConceptLabel.Text = m_ListConceptLabels[nListIndex];

            if (nListIndex == 0)
                btnPrev.Enabled = false;

            if (nListIndex < m_ListConceptLabels.Count)
                btnNext.Enabled = true;

            CheckForMinScore(m_ListConceptLabels[nListIndex]);
            PopulateTermGrid(m_ListConceptLabels[nListIndex]);

        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            nListIndex++;
            if (nListIndex <= m_ListConceptLabels.Count)
                lblConceptLabel.Text = m_ListConceptLabels[nListIndex];

            if (nListIndex == m_ListConceptLabels.Count-1)
                btnNext.Enabled = false;

            if (nListIndex > 0)
                btnPrev.Enabled = true;

            CheckForMinScore(m_ListConceptLabels[nListIndex]);
            PopulateTermGrid(m_ListConceptLabels[nListIndex]);

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void CheckForMinScore(string strCurrentLabel)
        {
            string[] strParts = strCurrentLabel.Split('(', ')');
            double dLabelScore = Convert.ToDouble(strParts[1]);
            if (dLabelScore < (double)m_fMinConceptScore)
                lblMinScoreNote.Visible = true;
            else
                lblMinScoreNote.Visible = false;
        }

        private void PopulateTermGrid(string strCurrentLabel)
        {
            dgConceptTerms.Rows.Clear();
            bool bTermsForLabel = false;

            foreach (string aLine in m_ConceptMatches.Split('\n'))
            {
                if (bTermsForLabel && !aLine.StartsWith("\t"))
                    bTermsForLabel = false;

                if (bTermsForLabel)
                {
                    string[] strParts = aLine.Split('(', ')');
                    dgConceptTerms.Rows.Add(strParts[0], strParts[1]);
                }

                if ((aLine.CompareTo(strCurrentLabel) == 0) && !bTermsForLabel)
                {
                    if (aLine.CompareTo(strCurrentLabel) == 0)
                        bTermsForLabel = true;
                }
            }
        }
    }
}
